package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.domain.DatInfo;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.mapper.DatInfoMapper;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.PartsMapper;
import com.ruoyi.system.service.IDatInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author wxd
 * @date 2024-08-24
 */
@Service
public class DatInfoServiceImpl implements IDatInfoService {
    @Autowired
    private DatInfoMapper datInfoMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private PartsMapper partsMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param DatID 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DatInfo selectDatInfoByDatID(Long DatID) {
        return datInfoMapper.selectDatInfoByDatID(DatID);
    }

    @Override
    public DatInfo selectOne(Long DatID, String deviceId) {
        return datInfoMapper.selectOne(DatID,deviceId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param datInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DatInfo> selectDatInfoList(DatInfo datInfo) {
        List<DatInfo> infos = datInfoMapper.selectDatInfoList(datInfo);
        List<String> deIds = infos.stream().map(DatInfo::getDeviceId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(deIds)) {
            List<Device> devices = deviceMapper.selectDeviceByIds(deIds.toArray(new String[0]));
            if (CollectionUtil.isNotEmpty(devices)) {
                Map<String, Device> map = devices.stream().collect(Collectors.toMap(Device::getId, p -> p));
                infos.forEach(x -> {
                    if (StrUtil.isNotEmpty(x.getDeviceId())) {
                        Device device = map.get(x.getDeviceId());
                        if (device != null) {
                            x.setDeviceModel(device.getModel());
                            x.setDeviceName(device.getName());
                        }
                    }
                });
            }
        }

        return infos;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param datInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDatInfo(DatInfo datInfo) {
        int i = datInfoMapper.insertDatInfo(datInfo);
        if (i <= 0) {
            return i;
        }
        if (StrUtil.isNotBlank(datInfo.getDeviceId())) {
            Device device = deviceMapper.selectDeviceById(datInfo.getDeviceId());
            if (device != null) {
                if (StrUtil.isNotBlank(device.getHtId())) {
                    partsMapper.updateTimesById(device.getHtId());
                }
                if (StrUtil.isNotBlank(device.getHnqId())) {
                    partsMapper.updateTimesById(device.getHnqId());
                }
                if (StrUtil.isNotBlank(device.getTfqId())) {
                    partsMapper.updateTimesById(device.getTfqId());
                }
                if (StrUtil.isNotBlank(device.getJzId())) {
                    partsMapper.updateTimesById(device.getJzId());
                }
                if (StrUtil.isNotBlank(device.getJz2Id())) {
                    partsMapper.updateTimesById(device.getJz2Id());
                }
                if (StrUtil.isNotBlank(device.getDmId())) {
                    partsMapper.updateTimesById(device.getDmId());
                }
                if (StrUtil.isNotBlank(device.getDxkId())) {
                    partsMapper.updateTimesById(device.getDxkId());
                }
                if (StrUtil.isNotBlank(device.getFxcId())) {
                    partsMapper.updateTimesById(device.getFxcId());
                }
                if (StrUtil.isNotBlank(device.getFsqId())) {
                    partsMapper.updateTimesById(device.getFsqId());
                }
            } else {
                device = new Device();
                device.setId(datInfo.getDeviceId());
                deviceMapper.insertDevice(device);
            }
        }
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param datInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDatInfo(DatInfo datInfo) {
        return datInfoMapper.updateDatInfo(datInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param DatIDs 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDatInfoByDatIDs(Long[] DatIDs) {
        return datInfoMapper.deleteDatInfoByDatIDs(DatIDs);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param DatID 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDatInfoByDatID(Long DatID) {
        return datInfoMapper.deleteDatInfoByDatID(DatID);
    }

    @Override
    public List<String> selectFieldGroup(String fieldName) {
        return datInfoMapper.selectFieldGroup(fieldName);
    }
}
